package tech.abing.spider.task.crawler.amac;

import java.util.Arrays;
import java.util.List;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.constants.TaskConstants;
import tech.abing.spider.task.crawler.amac.DirectInvestmentFundSpider.DynamicEntranceImpl;
import tech.abing.spider.task.pageprocessor.amac.DisciplinaryActionProcessor;
import tech.abing.spider.task.pageprocessor.amac.FalseFillProcessor;
import tech.abing.spider.task.pipeline.amac.DirectInvestmentFundPipeline;
import tech.abing.spider.task.pipeline.amac.IntegrityPublicityPipeline;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.SqlUtil;

public class FalseFillSpider {

	private static SqlUtil sqlUtil = null;
	/**
	 * @param args
	 */
	public static void start(){
		CrawlManager falseFillInfo = CrawlManager.getInstance();
		CrawTaskBuilder builder = falseFillInfo
		.prepareCrawlTask(DictEnum.TASK_TYPE_FALSE_FILL.getName(), DefaultPageDownloader.class)
		.useThread(1)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.usePageRetryCount(3)
		.setPolitenessDelay(1000)
		.usePipeline(IntegrityPublicityPipeline.class)
		.useTaskLifeListener(new TaskLifeListener(){

			@Override
			public void onStart(CrawlTask task) {
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_INTEGRITY_PUBLICITY.getCode()+" where 1=1 and PUBLICITY_TYPE = '"+DictEnum.TASK_TYPE_FALSE_FILL.getName()+"'");
				System.out.println("================开始");
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				System.out.println("================完成");
			}
		});

		CrawlTask task = builder.build();
		
		falseFillInfo.start(task);

	}
	
	public static final class DynamicEntranceImpl extends DynamicEntrance{

		@Override
		public List<StartContext> loadStartContext() {
			StartContext context = new StartContext();
			
			String url = "https://pf.amac.org.cn/open/comNotice/category";
			
			PageRequest req = context.createPageRequest(url, FalseFillProcessor.class);
			req.setMethod(Method.POST);
			req.setPostType(PostType.FORMDATA);
			
			req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
			req.putHeader("Cache-Control", "no-cache");
			req.putHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			req.putHeader("Host", "pf.amac.org.cn");
			req.putHeader("Referer", "https://pf.amac.org.cn/open/comNotice/category");
			req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36");
			req.putHeader("X-Requested-With", "XMLHttpRequest");
			
			req.putParams("userId", "");
			req.putParams("query_orgNameInitial", "");
			req.putParams("query_mainInvestDirection", "");
			req.putParams("query_fundSizeLong", "");
			req.putParams("query_specialTips", "");
			req.putParams("query_illegalProblem", "65002");
			req.putParams("query_orgName", "");
			req.putParams("pageSize", "100");
			
			
			context.injectSeed(req);
			
			return Arrays.asList(context);
		}
		
	}

}
